package app.caihan.wanandroid.ui.find

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import app.caihan.scframe.api.retrofit2.checkResult
import app.caihan.scframe.base.viewmodel.BaseViewModel
import app.caihan.wanandroid.model.ScWanAndroidFactory
import app.caihan.wanandroid.model.bean.Chapters

/**
 *
 *
 * @author Seven-C
 * @date 2020/10/17
 */
class KnowledgeListFragmentModel : BaseViewModel() {
    private val repository by lazy { ScWanAndroidFactory.makeWanRepository() }
    private var type: Int = 0

    // 私有的 MutableLiveData 可变的，对内访问
    private val _knowledgeList: MutableLiveData<ArrayList<Chapters>> by lazy {
        MutableLiveData<ArrayList<Chapters>>()
    }

    // 对外暴露不可变的 LiveData，只能查询
    val knowledgeList: LiveData<ArrayList<Chapters>> = _knowledgeList

    fun autoRefresh(id: Int) {
        type = id
        launchUI {
            showLoadingView()
            repository.getTreeList()
                .checkResult(
                    onSuccess = {
                        _knowledgeList.value = it
                    },
                    onError = {
                    })
            showContentView()
        }
    }
}